/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.yokeyword.sample.demo_flow.ui.fragment.home;

import me.yokeyword.sample.ResourceTable;

import me.yokeyword.sample.demo_flow.base.MySupportFraction;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;
import ohos.utils.PacMap;

public class DetailFraction extends MySupportFraction {
    private static final int REQ_MODIFY_FRACTION = 100;

    private static final String ARG_TITLE = "arg_title";
    static final String KEY_RESULT_TITLE = "title";
    private String mTitle;
    private Text mTcTitle;

    public static DetailFraction newInstance(String title) {
        DetailFraction fraction = new DetailFraction();
        PacMap bundle = new PacMap();
        bundle.putString(ARG_TITLE, title);
        fraction.setArguments(bundle);
        return fraction;
    }

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = scatter.parse(ResourceTable.Layout_layout_fraction_detail, container, false);
        initComponents(component);
        return component;
    }

    private void initComponents(Component component) {
        PacMap bundle = getArguments();
        if (bundle != null) {
            mTitle = bundle.getString(ARG_TITLE);
        }
        Text content = (Text) component.findComponentById(ResourceTable.Id_tv_content);
        content.setText(ResourceTable.String_large_text);
        Image img = (Image) component.findComponentById(ResourceTable.Id_fab);
        mTcTitle = (Text) component.findComponentById(ResourceTable.Id_tv_title);
        mTcTitle.setText(mTitle);
        img.setClickedListener(component1 -> startForResult(ModifyDetailFraction.newInstance(mTitle), REQ_MODIFY_FRACTION));
    }

    @Override
    public void onFractionResult(int requestCode, int resultCode, PacMap data) {
        super.onFractionResult(requestCode, resultCode, data);
        if (requestCode == REQ_MODIFY_FRACTION && resultCode == RESULT_OK && data != null) {
            mTitle = data.getString(KEY_RESULT_TITLE);
            getArguments().putString(ARG_TITLE, mTitle);
            mTcTitle.setText(mTitle);
            getFractionAbility().getUITaskDispatcher().asyncDispatch(() -> {
                ToastDialog toastDialog = new ToastDialog(getFractionAbility());
                toastDialog.setAutoClosable(false);
                toastDialog.setContentText("Modify the title successfully");
                toastDialog.show();
            });
        }
    }
}
